package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.entity.AttendanceClock;
import com.gxa.mapper.AttendenceMapper;
import com.gxa.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author 93024
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    AttendenceMapper mapper;

    /**
     * 添加一条考勤
     * @param attendanceClock
     * @return
     */
    @Override
    public R addAttendance(AttendanceClock attendanceClock) {

        //打卡状态status（1正常,2迟到，3早退）
        R r = new R();
        Byte status = 0;

        Byte type = attendanceClock.getType();
        status = getStatus(type);
        attendanceClock.setStatus(status);
        int row = mapper.addAttendence(attendanceClock);

        if (row > 0){
            r.setCode(200);
            r.setMsg("添加成功");
            return r;
        }else {
            r.setCode(400);
            r.setMsg("添加失败");
            return r;
        }
    }

    /**
     * 修改考勤打卡备注
     * @param id
     * @param memo
     * @return
     */
    @Override
    public R updateAttendance(Integer id, String memo) {

        R r = new R();
        int row = mapper.updateAttendance(id,memo);
        if (row > 0){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else {
            r.setCode(400);
            r.setMsg("修改失败");
            return r;
        }
    }

    /**
     * 查询所有考勤信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findAttendList(Integer currentPage, Integer pageSize) {
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<AttendanceClock> res = mapper.findAttendList();
        PageInfo<AttendanceClock> pageInfo = new PageInfo(res);
        System.out.println(pageInfo.getTotal());

        r.setCode(200);
        r.setMsg("查找成功");
        r.setData(pageInfo);
        return r;
    }

    /**
     * 根据id查找数据，用于回显
     * @param id
     * @return
     */
    @Override
    public R findAttendById(Integer id) {
        R r = new R();

        AttendanceClock res = mapper.findAttendById(id);
        if (res != null){
            r.setCode(200);
            r.setMsg("查找成功");
            r.setData(res);
            return r;
        }else {
            r.setCode(200);
            r.setMsg("没有数据，请重新输入id");
            return r;
        }
    }

    @Override
    public R delAttendById(Integer id) {
        R r = new R();
        int res2  = mapper.updateAttendance2(id);
        if (res2 > 0){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }
        r.setCode(400);
        r.setMsg("删除失败");
        return  r;
    }

    /**
     * 根据条件查询考勤
     * @param currentPage
     * @param pageSize
     * @param condition
     * @return
     */
    @Override
    public R findAttendListByCondition(Integer currentPage, Integer pageSize,String condition) {
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<AttendanceClock> res = mapper.findAttendListByCondition(condition);
        PageInfo<AttendanceClock> pageInfo = new PageInfo(res);
        System.out.println(pageInfo.getTotal());
        if (res.size()>0 || res.size()==0){
            r.setCode(200);
            r.setMsg("查找成功");
            r.setData(pageInfo);
            return r;
        }
        r.setCode(400);
        r.setMsg("查找失败");
        r.setData("参数错误");
        return r;
    }


    /**
     * 根据打卡获取状态
     * @param type
     * @return
     */
    private Byte getStatus(Byte type) {
        Byte status = 0;
        //打卡类型type（1上班，2下班，3加班，4外出，5请假）

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        String currenttime = sdf.format(calendar.getTime());
        try {
            Date current = sdf.parse(currenttime);
            System.out.println("当前时间:"+currenttime);
            switch(type)
            {
                case 1 :
                    //打卡为上班
                    //返回状态
                    status = normal(current);
                    System.out.println("打卡为上班");
                    break;
                case 2 :
                    //打卡为下班
                    //返回状态
                    status = outWork(current);
                    System.out.println("打卡为下班");
                    break;
                case 3 :
                    //打卡为加班
                    //返回状态
                    status = 1;
                    break;
                case 4 :
                    //打卡为外出
                    //返回状态
                    status = 1;
                    break;
                case 5 :
                    status = 1;
                    break;
                default :
                    System.out.println("未知错误");
            }
            return status;
        }catch (ParseException e){
            System.out.println("异常");
        }
        return status;
    }


    /**
     * 打卡下班
     * @param current
     * @return
     * @throws ParseException
     */
    private Byte outWork(Date current) throws ParseException {
        Byte status;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //上午下班时间
        Date workStart = sdf.parse("8:00:00");
        //判断上下午
        Date midday = sdf.parse("12:00:00");
        //下午上班时间
        Date afternoonStart = sdf.parse("14:00:00");
        //下午下班时间
        Date afternoonEnd = sdf.parse("18:00:00");
        if (current.compareTo(midday) < 0 && current.compareTo(midday) == 0) {
            //上午8.00-12.00
            if (current.compareTo(workStart) < 0 && current.compareTo(workStart) == 0) {
                System.out.println("早退");
                status=3;
                return status;
            }else{
                //上午8.00-12.00
                status=3;
                System.out.println("早退");
                return status;
            }
        }else {
            //下午
            if (current.compareTo(afternoonStart) < 0 && current.compareTo(afternoonStart) == 0) {
                System.out.println("早退");
                return status=3;
            }else if (current.compareTo(afternoonStart) > 0){
                //下午12.00-18.00迟到
                System.out.println("早退");
                status=3;
                return status;
            }else {
                System.out.println("正常");
                status=1;
                return status;
            }
        }
    }

    private Byte normal(Date current) throws ParseException {
        Byte status;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //上午下班时间
        Date workStart = sdf.parse("8:00:00");
        //判断上下午
        Date midday = sdf.parse("12:00:00");
        //下午上班时间
        Date afternoonStart = sdf.parse("14:00:00");
        //下午下班时间
        Date afternoonEnd = sdf.parse("18:00:00");
        if (current.compareTo(midday) < 0 && current.compareTo(midday) == 0) {
            //上午8.00-12.00
            if (current.compareTo(workStart) < 0 && current.compareTo(workStart) == 0) {
                System.out.println("正常上班");
                status=1;
                return status;
            }else{
                //上午8.00-12.00迟到
                status=2;
                System.out.println("迟到");
                return status;
            }
        }else {
            //下午
            if (current.compareTo(afternoonStart) < 0 && current.compareTo(afternoonStart) == 0) {
                System.out.println("正常上班");
                return status=1;
            }else if (current.compareTo(afternoonStart) > 0){
                //下午12.00-18.00迟到
                System.out.println("迟到");
                status=2;
                return status;
            }else {
                System.out.println("迟到");
                status=2;
                return status;
            }
        }
    }
}
